26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/errno.h>
33 #include <linux/list.h>
41 #include <linux/slab.h>
42 #include <asm/cacheflush.h>
51 #define DRIVER_VERSION "2009-05-26"
53 static const char hcd_name[] =
"r8a66597_hcd";
56 static int r8a66597_get_frame(
struct usb_hcd *hcd);
64 tmp = r8a66597_read(r8a66597,
INTENB0);
67 r8a66597_write(r8a66597, tmp,
INTENB0);
76 tmp = r8a66597_read(r8a66597,
INTENB0);
79 r8a66597_write(r8a66597, tmp,
INTENB0);
88 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
89 r8a66597_write(r8a66597, val, devadd_reg);
92 static int r8a66597_clock_enable(
struct r8a66597 *r8a66597)
97 if (r8a66597->
pdata->on_chip) {
101 tmp = r8a66597_read(r8a66597,
SYSCFG0);
106 }
while ((tmp &
SCKE) != SCKE);
107 r8a66597_write(r8a66597, 0x04, 0x02);
111 tmp = r8a66597_read(r8a66597,
SYSCFG0);
116 }
while ((tmp &
USBE) != USBE);
118 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->
pdata),
125 tmp = r8a66597_read(r8a66597,
SYSCFG0);
130 }
while ((tmp &
SCKE) != SCKE);
136 static void r8a66597_clock_disable(
struct r8a66597 *r8a66597)
141 if (r8a66597->
pdata->on_chip) {
150 static void r8a66597_enable_port(
struct r8a66597 *r8a66597,
int port)
163 static void r8a66597_disable_port(
struct r8a66597 *r8a66597,
int port)
167 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
168 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
170 r8a66597_port_power(r8a66597, port, 0);
182 static int enable_controller(
struct r8a66597 *r8a66597)
189 ret = r8a66597_clock_enable(r8a66597);
209 r8a66597_enable_port(r8a66597, port);
214 static void disable_controller(
struct r8a66597 *r8a66597)
219 r8a66597_write(r8a66597, 0,
INTENB0);
220 r8a66597_write(r8a66597, 0,
INTENB1);
221 r8a66597_write(r8a66597, 0,
BRDYENB);
222 r8a66597_write(r8a66597, 0,
BEMPENB);
223 r8a66597_write(r8a66597, 0,
NRDYENB);
226 r8a66597_write(r8a66597, 0,
BRDYSTS);
227 r8a66597_write(r8a66597, 0,
NRDYSTS);
228 r8a66597_write(r8a66597, 0,
BEMPSTS);
231 r8a66597_disable_port(r8a66597, port);
233 r8a66597_clock_disable(r8a66597);
236 static int get_parent_r8a66597_address(
struct r8a66597 *r8a66597,
237 struct usb_device *
udev)
241 if (udev->parent && udev->parent->devnum != 1)
251 static int is_child_device(
char *devpath)
253 return (devpath[2] ? 1 : 0);
256 static int is_hub_limit(
char *devpath)
258 return ((
strlen(devpath) >= 4) ? 1 : 0);
261 static void get_port_number(
struct r8a66597 *r8a66597,
265 *root_port = (devpath[0] & 0x0F) - 1;
270 *hub_port = devpath[2] & 0x0F;
295 static void set_child_connect_map(
struct r8a66597 *r8a66597,
int address)
303 static void put_child_connect_map(
struct r8a66597 *r8a66597,
int address)
313 u16 pipenum = pipe->
info.pipenum;
340 get_urb_to_r8a66597_dev(
struct r8a66597 *r8a66597,
struct urb *
urb)
342 if (usb_pipedevice(urb->pipe) == 0)
348 static int make_r8a66597_device(
struct r8a66597 *r8a66597,
359 dev->
udev = urb->dev;
368 get_port_number(r8a66597, urb->dev->devpath,
370 if (!is_child_device(urb->dev->devpath))
373 set_devadd_reg(r8a66597, dev->
address,
374 get_r8a66597_usb_speed(urb->dev->speed),
375 get_parent_r8a66597_address(r8a66597, urb->dev),
382 static u8 alloc_usb_address(
struct r8a66597 *r8a66597,
struct urb *urb)
387 if (is_hub_limit(urb->dev->devpath)) {
388 dev_err(&urb->dev->dev,
"External hub limit reached.\n");
392 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
400 dev_dbg(&urb->dev->dev,
"alloc_address: r8a66597_addr=%d\n", addr);
403 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
410 "cannot communicate with a USB device more than 10.(%x)\n",
417 static void free_usb_address(
struct r8a66597 *r8a66597,
441 if (r8a66597->
root_hub[port].dev == dev) {
448 static void r8a66597_reg_wait(
struct r8a66597 *r8a66597,
unsigned long reg,
455 tmp = r8a66597_read(r8a66597, reg);
458 "is timeout\n", reg, loop);
462 }
while ((tmp & mask) != loop);
466 static void pipe_start(
struct r8a66597 *r8a66597,
struct r8a66597_pipe *pipe)
470 tmp = r8a66597_read(r8a66597, pipe->
pipectr) &
PID;
477 static void pipe_stop(
struct r8a66597 *r8a66597,
struct r8a66597_pipe *pipe)
481 tmp = r8a66597_read(r8a66597, pipe->
pipectr) &
PID;
489 static void clear_all_buffer(
struct r8a66597 *r8a66597,
494 if (!pipe || pipe->
info.pipenum == 0)
497 pipe_stop(r8a66597, pipe);
499 tmp = r8a66597_read(r8a66597, pipe->
pipectr);
500 tmp = r8a66597_read(r8a66597, pipe->
pipectr);
501 tmp = r8a66597_read(r8a66597, pipe->
pipectr);
506 static void r8a66597_pipe_toggle(
struct r8a66597 *r8a66597,
515 static inline unsigned short mbw_value(
struct r8a66597 *r8a66597)
517 if (r8a66597->
pdata->on_chip)
524 static inline void cfifo_change(
struct r8a66597 *r8a66597,
u16 pipenum)
526 unsigned short mbw = mbw_value(r8a66597);
533 static inline void fifo_change_from_pipe(
struct r8a66597 *r8a66597,
536 unsigned short mbw = mbw_value(r8a66597);
538 cfifo_change(r8a66597, 0);
542 r8a66597_mdfy(r8a66597, mbw | pipe->
info.pipenum, mbw |
CURPIPE,
547 static u16 r8a66597_get_pipenum(
struct urb *urb,
struct usb_host_endpoint *
hep)
551 if (usb_pipeendpoint(urb->pipe) == 0)
554 return pipe->
info.pipenum;
557 static u16 get_urb_to_r8a66597_addr(
struct r8a66597 *r8a66597,
struct urb *urb)
561 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->
address;
574 static void pipe_toggle_set(
struct r8a66597 *r8a66597,
576 struct urb *urb,
int set)
579 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
580 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
586 *toggle |= 1 << endpoint;
588 *toggle &= ~(1 << endpoint);
592 static void pipe_toggle_save(
struct r8a66597 *r8a66597,
597 pipe_toggle_set(r8a66597, pipe, urb, 1);
599 pipe_toggle_set(r8a66597, pipe, urb, 0);
603 static void pipe_toggle_restore(
struct r8a66597 *r8a66597,
608 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
609 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
614 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
618 static void pipe_buffer_setting(
struct r8a66597 *r8a66597,
634 r8a66597_write(r8a66597, val,
PIPECFG);
644 static void pipe_setting(
struct r8a66597 *r8a66597,
struct r8a66597_td *
td)
647 struct urb *urb = td->
urb;
650 info = &td->
pipe->info;
651 cfifo_change(r8a66597, 0);
652 pipe_buffer_setting(r8a66597, info);
654 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
655 usb_pipeout(urb->pipe)) &&
656 !usb_pipecontrol(urb->pipe)) {
657 r8a66597_pipe_toggle(r8a66597, td->
pipe, 0);
658 pipe_toggle_set(r8a66597, td->
pipe, urb, 0);
659 clear_all_buffer(r8a66597, td->
pipe);
660 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
661 usb_pipeout(urb->pipe), 1);
663 pipe_toggle_restore(r8a66597, td->
pipe, urb);
668 static u16 get_empty_pipenum(
struct r8a66597 *r8a66597,
673 memset(array, 0,
sizeof(array));
674 switch (usb_endpoint_type(ep)) {
676 if (usb_endpoint_dir_in(ep))
684 if (usb_endpoint_dir_in(ep)) {
692 if (usb_endpoint_dir_in(ep))
704 while (array[i] != 0) {
729 r8a66597_type = 0x0000;
733 return r8a66597_type;
736 static u16 get_bufnum(
u16 pipenum)
745 bufnum = 4 + (pipenum - 6);
752 static u16 get_buf_bsize(
u16 pipenum)
769 static void enable_r8a66597_pipe_dma(
struct r8a66597 *r8a66597,
776 unsigned short mbw = mbw_value(r8a66597);
779 if (r8a66597->
pdata->on_chip)
784 if ((r8a66597->
dma_map & (1 << i)) != 0)
788 "address %d, EndpointAddress 0x%02x use "
789 "DMA FIFO\n", usb_pipedevice(urb->pipe),
796 set_pipe_reg_addr(pipe, i);
798 cfifo_change(r8a66597, 0);
799 r8a66597_mdfy(r8a66597, mbw | pipe->
info.pipenum,
811 static void enable_r8a66597_pipe(
struct r8a66597 *r8a66597,
struct urb *urb,
812 struct usb_host_endpoint *hep,
825 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
828 static void r8a66597_urb_done(
struct r8a66597 *r8a66597,
struct urb *urb,
833 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
836 for (ptr = urb->transfer_buffer;
843 spin_unlock(&r8a66597->
lock);
845 spin_lock(&r8a66597->
lock);
849 static void force_dequeue(
struct r8a66597 *r8a66597,
u16 pipenum,
u16 address)
855 if (list_empty(list))
867 r8a66597_urb_done(r8a66597, urb, -
ENODEV);
874 static void disable_r8a66597_pipe_all(
struct r8a66597 *r8a66597,
889 force_dequeue(r8a66597, 0, dev->
address);
894 force_dequeue(r8a66597, pipenum, dev->
address);
912 time = interval ? interval - 1 : 0;
914 if (interval > 128) {
918 for (i = 0; i < 7; i++) {
919 if ((1 << i) < interval &&
920 (1 << (i + 1) > interval))
929 static unsigned long get_timer_interval(
struct urb *urb,
__u8 interval)
932 unsigned long time = 1;
934 if (usb_pipeisoc(urb->pipe))
937 if (get_r8a66597_usb_speed(urb->dev->speed) ==
HSMODE) {
938 for (i = 0; i < (interval - 1); i++)
940 time = time * 125 / 1000;
949 static void init_pipe_info(
struct r8a66597 *r8a66597,
struct urb *urb,
950 struct usb_host_endpoint *hep,
955 info.
pipenum = get_empty_pipenum(r8a66597, ep);
956 info.
address = get_urb_to_r8a66597_addr(r8a66597, urb);
957 info.
epnum = usb_endpoint_num(ep);
959 info.
type = get_r8a66597_type(usb_endpoint_type(ep));
969 if (usb_endpoint_dir_in(ep))
974 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
977 static void init_pipe_config(
struct r8a66597 *r8a66597,
struct urb *urb)
981 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
985 static void pipe_irq_enable(
struct r8a66597 *r8a66597,
struct urb *urb,
988 if (pipenum == 0 && usb_pipeout(urb->pipe))
993 if (!usb_pipeisoc(urb->pipe))
997 static void pipe_irq_disable(
struct r8a66597 *r8a66597,
u16 pipenum)
1003 static void r8a66597_root_hub_start_polling(
struct r8a66597 *r8a66597)
1009 static void start_root_hub_sampling(
struct r8a66597 *r8a66597,
int port,
1014 rh->
old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) &
LNST;
1022 r8a66597_root_hub_start_polling(r8a66597);
1026 static void r8a66597_check_syssts(
struct r8a66597 *r8a66597,
int port,
1031 if (syssts ==
SE0) {
1032 r8a66597_write(r8a66597, ~
ATTCH, get_intsts_reg(port));
1040 r8a66597_write(r8a66597, ~
DTCH, get_intsts_reg(port));
1044 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1047 spin_unlock(&r8a66597->
lock);
1049 spin_lock(&r8a66597->
lock);
1053 static void r8a66597_usb_connect(
struct r8a66597 *r8a66597,
int port)
1055 u16 speed = get_rh_usb_speed(r8a66597, port);
1061 else if (speed ==
LSMODE)
1069 static void r8a66597_usb_disconnect(
struct r8a66597 *r8a66597,
int port)
1073 disable_r8a66597_pipe_all(r8a66597, dev);
1074 free_usb_address(r8a66597, dev, 0);
1076 start_root_hub_sampling(r8a66597, port, 0);
1080 static void prepare_setup_packet(
struct r8a66597 *r8a66597,
1085 unsigned long setup_addr =
USBREQ;
1091 for (i = 0; i < 4; i++) {
1092 r8a66597_write(r8a66597,
le16_to_cpu(p[i]), setup_addr);
1099 static void prepare_packet_read(
struct r8a66597 *r8a66597,
1102 struct urb *urb = td->
urb;
1104 if (usb_pipecontrol(urb->pipe)) {
1108 if (urb->actual_length == 0) {
1109 r8a66597_pipe_toggle(r8a66597, td->
pipe, 1);
1112 pipe_irq_disable(r8a66597, td->
pipenum);
1113 pipe_start(r8a66597, td->
pipe);
1114 pipe_irq_enable(r8a66597, urb, td->
pipenum);
1116 if (urb->actual_length == 0) {
1117 pipe_irq_disable(r8a66597, td->
pipenum);
1118 pipe_setting(r8a66597, td);
1119 pipe_stop(r8a66597, td->
pipe);
1122 if (td->
pipe->pipetre) {
1123 r8a66597_write(r8a66597,
TRCLR,
1125 r8a66597_write(r8a66597,
1127 (urb->transfer_buffer_length,
1134 pipe_start(r8a66597, td->
pipe);
1135 pipe_irq_enable(r8a66597, urb, td->
pipenum);
1141 static void prepare_packet_write(
struct r8a66597 *r8a66597,
1145 struct urb *urb = td->
urb;
1147 if (usb_pipecontrol(urb->pipe)) {
1148 pipe_stop(r8a66597, td->
pipe);
1152 if (urb->actual_length == 0) {
1153 r8a66597_pipe_toggle(r8a66597, td->
pipe, 1);
1157 if (urb->actual_length == 0)
1158 pipe_setting(r8a66597, td);
1159 if (td->
pipe->pipetre)
1164 fifo_change_from_pipe(r8a66597, td->
pipe);
1165 tmp = r8a66597_read(r8a66597, td->
pipe->fifoctr);
1167 pipe_irq_enable(r8a66597, urb, td->
pipenum);
1169 packet_write(r8a66597, td->
pipenum);
1170 pipe_start(r8a66597, td->
pipe);
1174 static void prepare_status_packet(
struct r8a66597 *r8a66597,
1177 struct urb *urb = td->
urb;
1179 r8a66597_pipe_toggle(r8a66597, td->
pipe, 1);
1180 pipe_stop(r8a66597, td->
pipe);
1197 pipe_start(r8a66597, td->
pipe);
1200 static int is_set_address(
unsigned char *setup_packet)
1210 static int start_transfer(
struct r8a66597 *r8a66597,
struct r8a66597_td *td)
1216 if (is_set_address(td->
urb->setup_packet)) {
1218 td->
urb->setup_packet[2] = alloc_usb_address(r8a66597,
1220 if (td->
urb->setup_packet[2] == 0)
1223 prepare_setup_packet(r8a66597, td);
1226 prepare_packet_read(r8a66597, td);
1229 prepare_packet_write(r8a66597, td);
1232 prepare_status_packet(r8a66597, td);
1242 static int check_transfer_finish(
struct r8a66597_td *td,
struct urb *urb)
1244 if (usb_pipeisoc(urb->pipe)) {
1245 if (urb->number_of_packets == td->
iso_cnt)
1250 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1258 static void set_td_timer(
struct r8a66597 *r8a66597,
struct r8a66597_td *td)
1265 !usb_pipecontrol(td->
urb->pipe) && usb_pipein(td->
urb->pipe)) {
1267 switch (usb_pipetype(td->
urb->pipe)) {
1268 case PIPE_INTERRUPT:
1269 case PIPE_ISOCHRONOUS:
1283 static void finish_request(
struct r8a66597 *r8a66597,
struct r8a66597_td *td,
1284 u16 pipenum,
struct urb *urb,
int status)
1288 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1294 r8a66597->
address_map &= ~(1 << urb->setup_packet[2]);
1296 pipe_toggle_save(r8a66597, td->
pipe, urb);
1301 if (!list_empty(&r8a66597->
pipe_queue[pipenum]))
1305 if (usb_pipeisoc(urb->pipe))
1306 urb->start_frame = r8a66597_get_frame(hcd);
1308 r8a66597_urb_done(r8a66597, urb,
status);
1312 td = r8a66597_get_td(r8a66597, pipenum);
1316 start_transfer(r8a66597, td);
1317 set_td_timer(r8a66597, td);
1321 static void packet_read(
struct r8a66597 *r8a66597,
u16 pipenum)
1326 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1335 fifo_change_from_pipe(r8a66597, td->
pipe);
1336 tmp = r8a66597_read(r8a66597, td->
pipe->fifoctr);
1338 pipe_stop(r8a66597, td->
pipe);
1339 pipe_irq_disable(r8a66597, pipenum);
1341 finish_request(r8a66597, td, pipenum, td->
urb, -
EPIPE);
1346 rcv_len = tmp &
DTLN;
1347 if (usb_pipeisoc(urb->pipe)) {
1348 buf = (
u16 *)(urb->transfer_buffer +
1349 urb->iso_frame_desc[td->
iso_cnt].offset);
1350 urb_len = urb->iso_frame_desc[td->
iso_cnt].length;
1352 buf = (
void *)urb->transfer_buffer + urb->actual_length;
1353 urb_len = urb->transfer_buffer_length - urb->actual_length;
1356 if (rcv_len <= bufsize) {
1365 urb->actual_length +=
size;
1368 if (rcv_len < bufsize) {
1371 if (usb_pipeisoc(urb->pipe)) {
1372 urb->iso_frame_desc[td->
iso_cnt].actual_length =
size;
1379 if (finish || check_transfer_finish(td, urb)) {
1380 pipe_stop(r8a66597, td->
pipe);
1381 pipe_irq_disable(r8a66597, pipenum);
1386 if (urb->transfer_buffer) {
1388 r8a66597_write(r8a66597,
BCLR, td->
pipe->fifoctr);
1390 r8a66597_read_fifo(r8a66597, td->
pipe->fifoaddr,
1394 if (finish && pipenum != 0)
1395 finish_request(r8a66597, td, pipenum, urb, status);
1398 static void packet_write(
struct r8a66597 *r8a66597,
u16 pipenum)
1403 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1410 fifo_change_from_pipe(r8a66597, td->
pipe);
1411 tmp = r8a66597_read(r8a66597, td->
pipe->fifoctr);
1413 pipe_stop(r8a66597, td->
pipe);
1414 pipe_irq_disable(r8a66597, pipenum);
1416 finish_request(r8a66597, td, pipenum, urb, -
EPIPE);
1422 if (usb_pipeisoc(urb->pipe)) {
1423 buf = (
u16 *)(urb->transfer_buffer +
1424 urb->iso_frame_desc[td->
iso_cnt].offset);
1426 (
int)urb->iso_frame_desc[td->
iso_cnt].length);
1428 buf = (
u16 *)(urb->transfer_buffer + urb->actual_length);
1430 urb->transfer_buffer_length - urb->actual_length);
1435 r8a66597_write(r8a66597, ~(1 << pipenum),
BEMPSTS);
1436 if (urb->transfer_buffer) {
1437 r8a66597_write_fifo(r8a66597, td->
pipe, buf, size);
1438 if (!usb_pipebulk(urb->pipe) || td->
maxpacket != size)
1439 r8a66597_write(r8a66597,
BVAL, td->
pipe->fifoctr);
1443 urb->actual_length +=
size;
1444 if (usb_pipeisoc(urb->pipe)) {
1445 urb->iso_frame_desc[td->
iso_cnt].actual_length =
size;
1446 urb->iso_frame_desc[td->
iso_cnt].status = 0;
1451 if (check_transfer_finish(td, urb)) {
1454 if (!usb_pipeisoc(urb->pipe))
1457 pipe_irq_enable(r8a66597, urb, pipenum);
1461 static void check_next_phase(
struct r8a66597 *r8a66597,
int status)
1463 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1474 if (check_transfer_finish(td, urb))
1478 if (urb->transfer_buffer_length == urb->actual_length)
1480 else if (usb_pipeout(urb->pipe))
1490 if (finish || status != 0 || urb->unlinked)
1491 finish_request(r8a66597, td, 0, urb, status);
1493 start_transfer(r8a66597, td);
1496 static int get_urb_error(
struct r8a66597 *r8a66597,
u16 pipenum)
1498 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1511 static void irq_pipe_ready(
struct r8a66597 *r8a66597)
1518 mask = r8a66597_read(r8a66597,
BRDYSTS)
1519 & r8a66597_read(r8a66597,
BRDYENB);
1520 r8a66597_write(r8a66597, ~mask,
BRDYSTS);
1522 td = r8a66597_get_td(r8a66597, 0);
1524 packet_read(r8a66597, 0);
1526 pipe_irq_disable(r8a66597, 0);
1527 check_next_phase(r8a66597, 0);
1533 td = r8a66597_get_td(r8a66597, pipenum);
1538 packet_read(r8a66597, pipenum);
1540 packet_write(r8a66597, pipenum);
1545 static void irq_pipe_empty(
struct r8a66597 *r8a66597)
1553 mask = r8a66597_read(r8a66597,
BEMPSTS)
1554 & r8a66597_read(r8a66597,
BEMPENB);
1555 r8a66597_write(r8a66597, ~mask,
BEMPSTS);
1557 cfifo_change(r8a66597, 0);
1558 td = r8a66597_get_td(r8a66597, 0);
1561 check_next_phase(r8a66597, 0);
1568 td = r8a66597_get_td(r8a66597, pipenum);
1572 tmp = r8a66597_read(r8a66597, td->
pipe->pipectr);
1573 if ((tmp &
INBUFM) == 0) {
1575 pipe_irq_disable(r8a66597, pipenum);
1576 finish_request(r8a66597, td, pipenum, td->
urb,
1583 static void irq_pipe_nrdy(
struct r8a66597 *r8a66597)
1590 mask = r8a66597_read(r8a66597,
NRDYSTS)
1591 & r8a66597_read(r8a66597,
NRDYENB);
1592 r8a66597_write(r8a66597, ~mask,
NRDYSTS);
1594 cfifo_change(r8a66597, 0);
1595 status = get_urb_error(r8a66597, 0);
1596 pipe_irq_disable(r8a66597, 0);
1597 check_next_phase(r8a66597, status);
1604 td = r8a66597_get_td(r8a66597, pipenum);
1608 status = get_urb_error(r8a66597, pipenum);
1609 pipe_irq_disable(r8a66597, pipenum);
1610 pipe_stop(r8a66597, td->
pipe);
1611 finish_request(r8a66597, td, pipenum, td->
urb, status);
1616 static irqreturn_t r8a66597_irq(
struct usb_hcd *hcd)
1618 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1619 u16 intsts0, intsts1, intsts2;
1620 u16 intenb0, intenb1, intenb2;
1621 u16 mask0, mask1, mask2;
1624 spin_lock(&r8a66597->
lock);
1626 intsts0 = r8a66597_read(r8a66597,
INTSTS0);
1627 intsts1 = r8a66597_read(r8a66597,
INTSTS1);
1628 intsts2 = r8a66597_read(r8a66597,
INTSTS2);
1629 intenb0 = r8a66597_read(r8a66597,
INTENB0);
1630 intenb1 = r8a66597_read(r8a66597,
INTENB1);
1631 intenb2 = r8a66597_read(r8a66597,
INTENB2);
1633 mask2 = intsts2 & intenb2;
1634 mask1 = intsts1 & intenb1;
1637 if (mask2 &
ATTCH) {
1638 r8a66597_write(r8a66597, ~ATTCH,
INTSTS2);
1642 start_root_hub_sampling(r8a66597, 1, 1);
1645 r8a66597_write(r8a66597, ~DTCH,
INTSTS2);
1647 r8a66597_usb_disconnect(r8a66597, 1);
1650 r8a66597_write(r8a66597, ~BCHG,
INTSTS2);
1652 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1657 if (mask1 & ATTCH) {
1658 r8a66597_write(r8a66597, ~ATTCH,
INTSTS1);
1662 start_root_hub_sampling(r8a66597, 0, 1);
1665 r8a66597_write(r8a66597, ~DTCH,
INTSTS1);
1667 r8a66597_usb_disconnect(r8a66597, 0);
1670 r8a66597_write(r8a66597, ~BCHG,
INTSTS1);
1672 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1676 r8a66597_write(r8a66597, ~SIGN,
INTSTS1);
1677 status = get_urb_error(r8a66597, 0);
1678 check_next_phase(r8a66597, status);
1681 r8a66597_write(r8a66597, ~SACK,
INTSTS1);
1682 check_next_phase(r8a66597, 0);
1687 irq_pipe_ready(r8a66597);
1689 irq_pipe_empty(r8a66597);
1691 irq_pipe_nrdy(r8a66597);
1694 spin_unlock(&r8a66597->
lock);
1699 static void r8a66597_root_hub_control(
struct r8a66597 *r8a66597,
int port)
1705 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1707 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1708 if ((tmp &
USBRST) == USBRST) {
1709 r8a66597_mdfy(r8a66597,
UACT, USBRST |
UACT,
1711 r8a66597_root_hub_start_polling(r8a66597);
1713 r8a66597_usb_connect(r8a66597, port);
1717 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1722 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) &
LNST;
1726 r8a66597_check_syssts(r8a66597, port, tmp);
1728 r8a66597_root_hub_start_polling(r8a66597);
1732 r8a66597_root_hub_start_polling(r8a66597);
1737 static void r8a66597_interval_timer(
unsigned long _r8a66597)
1739 struct r8a66597 *r8a66597 = (
struct r8a66597 *)_r8a66597;
1740 unsigned long flags;
1752 td = r8a66597_get_td(r8a66597, pipenum);
1754 start_transfer(r8a66597, td);
1757 spin_unlock_irqrestore(&r8a66597->
lock, flags);
1760 static void r8a66597_td_timer(
unsigned long _r8a66597)
1762 struct r8a66597 *r8a66597 = (
struct r8a66597 *)_r8a66597;
1763 unsigned long flags;
1772 if (timer_pending(&r8a66597->
td_timer[pipenum]))
1775 td = r8a66597_get_td(r8a66597, pipenum);
1781 if (td->
urb->actual_length) {
1782 set_td_timer(r8a66597, td);
1787 pipe_stop(r8a66597, pipe);
1791 list_move_tail(&new_td->
queue,
1793 new_td = r8a66597_get_td(r8a66597, pipenum);
1800 start_transfer(r8a66597, new_td);
1805 set_td_timer(r8a66597, new_td);
1808 spin_unlock_irqrestore(&r8a66597->
lock, flags);
1811 static void r8a66597_timer(
unsigned long _r8a66597)
1813 struct r8a66597 *r8a66597 = (
struct r8a66597 *)_r8a66597;
1814 unsigned long flags;
1820 r8a66597_root_hub_control(r8a66597, port);
1822 spin_unlock_irqrestore(&r8a66597->
lock, flags);
1825 static int check_pipe_config(
struct r8a66597 *r8a66597,
struct urb *urb)
1836 static int r8a66597_start(
struct usb_hcd *hcd)
1838 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1840 hcd->state = HC_STATE_RUNNING;
1841 return enable_controller(r8a66597);
1844 static void r8a66597_stop(
struct usb_hcd *hcd)
1846 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1848 disable_controller(r8a66597);
1851 static void set_address_zero(
struct r8a66597 *r8a66597,
struct urb *urb)
1853 unsigned int usb_address = usb_pipedevice(urb->pipe);
1854 u16 root_port, hub_port;
1856 if (usb_address == 0) {
1857 get_port_number(r8a66597, urb->dev->devpath,
1858 &root_port, &hub_port);
1859 set_devadd_reg(r8a66597, 0,
1860 get_r8a66597_usb_speed(urb->dev->speed),
1861 get_parent_r8a66597_address(r8a66597, urb->dev),
1862 hub_port, root_port);
1866 static struct r8a66597_td *r8a66597_make_td(
struct r8a66597 *r8a66597,
1868 struct usb_host_endpoint *hep)
1877 pipenum = r8a66597_get_pipenum(urb, hep);
1879 td->
pipe = hep->hcpriv;
1881 td->
address = get_urb_to_r8a66597_addr(r8a66597, urb);
1882 td->
maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1883 !usb_pipein(urb->pipe));
1884 if (usb_pipecontrol(urb->pipe))
1886 else if (usb_pipein(urb->pipe))
1890 INIT_LIST_HEAD(&td->
queue);
1895 static int r8a66597_urb_enqueue(
struct usb_hcd *hcd,
1899 struct usb_host_endpoint *hep = urb->ep;
1900 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1903 unsigned long flags;
1906 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1908 goto error_not_linked;
1913 goto error_not_linked;
1923 if (usb_pipeendpoint(urb->pipe))
1924 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1927 if (
unlikely(check_pipe_config(r8a66597, urb)))
1928 init_pipe_config(r8a66597, urb);
1930 set_address_zero(r8a66597, urb);
1931 td = r8a66597_make_td(r8a66597, urb, hep);
1942 if (td->
pipe->info.timer_interval) {
1946 td->
pipe->info.timer_interval));
1948 ret = start_transfer(r8a66597, td);
1955 set_td_timer(r8a66597, td);
1961 spin_unlock_irqrestore(&r8a66597->
lock, flags);
1965 static int r8a66597_urb_dequeue(
struct usb_hcd *hcd,
struct urb *urb,
1968 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1970 unsigned long flags;
1980 pipe_stop(r8a66597, td->
pipe);
1981 pipe_irq_disable(r8a66597, td->
pipenum);
1983 finish_request(r8a66597, td, td->
pipenum, urb, status);
1986 spin_unlock_irqrestore(&r8a66597->
lock, flags);
1990 static void r8a66597_endpoint_disable(
struct usb_hcd *hcd,
1991 struct usb_host_endpoint *hep)
1993 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1996 struct urb *urb =
NULL;
1998 unsigned long flags;
2002 pipenum = pipe->
info.pipenum;
2011 pipe_stop(r8a66597, pipe);
2012 pipe_irq_disable(r8a66597, pipenum);
2014 td = r8a66597_get_td(r8a66597, pipenum);
2017 finish_request(r8a66597, td, pipenum, urb, -
ESHUTDOWN);
2020 spin_unlock_irqrestore(&r8a66597->
lock, flags);
2023 static int r8a66597_get_frame(
struct usb_hcd *hcd)
2025 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2026 return r8a66597_read(r8a66597,
FRMNUM) & 0x03FF;
2029 static void collect_usb_address_map(
struct usb_device *udev,
unsigned long *
map)
2032 struct usb_device *childdev;
2035 udev->parent && udev->parent->devnum > 1 &&
2037 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
2039 usb_hub_for_each_child(udev, chix, childdev) {
2041 collect_usb_address_map(childdev, map);
2046 static struct r8a66597_device *get_r8a66597_device(
struct r8a66597 *r8a66597,
2059 printk(
KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
2063 static void update_usb_address_map(
struct r8a66597 *r8a66597,
2064 struct usb_device *root_hub,
2069 unsigned long flags;
2071 for (i = 0; i < 4; i++) {
2076 for (j = 0; j < 32; j++) {
2077 if (!(diff & (1 << j)))
2081 if (map[i] & (1 << j))
2082 set_child_connect_map(r8a66597, addr);
2087 dev = get_r8a66597_device(r8a66597, addr);
2088 disable_r8a66597_pipe_all(r8a66597, dev);
2089 free_usb_address(r8a66597, dev, 0);
2090 put_child_connect_map(r8a66597, addr);
2091 spin_unlock_irqrestore(&r8a66597->
lock, flags);
2097 static void r8a66597_check_detect_child(
struct r8a66597 *r8a66597,
2098 struct usb_hcd *hcd)
2100 struct usb_bus *
bus;
2101 unsigned long now_map[4];
2103 memset(now_map, 0,
sizeof(now_map));
2109 if (bus->busnum != hcd->self.busnum)
2112 collect_usb_address_map(bus->root_hub, now_map);
2113 update_usb_address_map(r8a66597, bus->
root_hub, now_map);
2117 static int r8a66597_hub_status_data(
struct usb_hcd *hcd,
char *buf)
2119 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2120 unsigned long flags;
2123 r8a66597_check_detect_child(r8a66597, hcd);
2130 if (r8a66597->
root_hub[i].port & 0xffff0000)
2131 *buf |= 1 << (i + 1);
2134 spin_unlock_irqrestore(&r8a66597->
lock, flags);
2139 static void r8a66597_hub_descriptor(
struct r8a66597 *r8a66597,
2153 static int r8a66597_hub_control(
struct usb_hcd *hcd,
u16 typeReq,
u16 wValue,
2156 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2158 int port = (wIndex & 0x00FF) - 1;
2160 unsigned long flags;
2166 case ClearHubFeature:
2176 case ClearPortFeature:
2189 r8a66597_port_power(r8a66597, port, 0);
2202 case GetHubDescriptor:
2203 r8a66597_hub_descriptor(r8a66597,
2214 case SetPortFeature:
2224 r8a66597_port_power(r8a66597, port, 1);
2232 disable_r8a66597_pipe_all(r8a66597, dev);
2233 free_usb_address(r8a66597, dev, 1);
2236 get_dvstctr_reg(port));
2252 spin_unlock_irqrestore(&r8a66597->
lock, flags);
2256 #if defined(CONFIG_PM)
2259 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2264 for (port = 0; port < r8a66597->
max_root_hub; port++) {
2266 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2271 dev_dbg(&rh->
dev->udev->dev,
"suspend port = %d\n", port);
2275 if (rh->
dev->udev->do_remote_wakeup) {
2278 r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
2290 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2295 for (port = 0; port < r8a66597->
max_root_hub; port++) {
2297 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2302 dev_dbg(&rh->
dev->udev->dev,
"resume port = %d\n", port);
2314 #define r8a66597_bus_suspend NULL
2315 #define r8a66597_bus_resume NULL
2318 static struct hc_driver r8a66597_hc_driver = {
2319 .description = hcd_name,
2320 .hcd_priv_size =
sizeof(
struct r8a66597),
2321 .irq = r8a66597_irq,
2328 .start = r8a66597_start,
2329 .stop = r8a66597_stop,
2334 .urb_enqueue = r8a66597_urb_enqueue,
2335 .urb_dequeue = r8a66597_urb_dequeue,
2336 .endpoint_disable = r8a66597_endpoint_disable,
2341 .get_frame_number = r8a66597_get_frame,
2346 .hub_status_data = r8a66597_hub_status_data,
2347 .hub_control = r8a66597_hub_control,
2352 #if defined(CONFIG_PM)
2353 static int r8a66597_suspend(
struct device *dev)
2358 dev_dbg(dev,
"%s\n", __func__);
2360 disable_controller(r8a66597);
2362 for (port = 0; port < r8a66597->
max_root_hub; port++) {
2365 rh->
port = 0x00000000;
2371 static int r8a66597_resume(
struct device *dev)
2374 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2376 dev_dbg(dev,
"%s\n", __func__);
2378 enable_controller(r8a66597);
2379 usb_root_hub_lost_power(hcd->self.root_hub);
2384 static const struct dev_pm_ops r8a66597_dev_pm_ops = {
2386 .resume = r8a66597_resume,
2387 .poweroff = r8a66597_suspend,
2388 .restore = r8a66597_resume,
2391 #define R8A66597_DEV_PM_OPS (&r8a66597_dev_pm_ops)
2393 #define R8A66597_DEV_PM_OPS NULL
2399 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2404 if (r8a66597->
pdata->on_chip)
2416 struct usb_hcd *hcd =
NULL;
2417 struct r8a66597 *r8a66597;
2420 unsigned long irq_trigger;
2425 if (pdev->
dev.dma_mask) {
2434 dev_err(&pdev->
dev,
"platform_get_resource error.\n");
2442 "platform_get_resource IORESOURCE_IRQ error.\n");
2456 if (pdev->
dev.platform_data ==
NULL) {
2466 dev_err(&pdev->
dev,
"Failed to create hcd\n");
2469 r8a66597 = hcd_to_r8a66597(hcd);
2470 memset(r8a66597, 0,
sizeof(
struct r8a66597));
2472 r8a66597->
pdata = pdev->
dev.platform_data;
2475 if (r8a66597->
pdata->on_chip) {
2476 snprintf(clk_name,
sizeof(clk_name),
"usb%d", pdev->
id);
2478 if (IS_ERR(r8a66597->
clk)) {
2479 dev_err(&pdev->
dev,
"cannot get clock \"%s\"\n",
2481 ret = PTR_ERR(r8a66597->
clk);
2490 r8a66597->
rh_timer.function = r8a66597_timer;
2495 ret = r8a66597_clock_enable(r8a66597);
2498 disable_controller(r8a66597);
2503 r8a66597->
td_timer[
i].function = r8a66597_td_timer;
2506 r8a66597_interval_timer,
2507 (
unsigned long)r8a66597);
2511 hcd->rsrc_start = res->
start;
2523 if (r8a66597->
pdata->on_chip)
2536 .probe = r8a66597_probe,
2539 .name = (
char *) hcd_name,